﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BLL;
using Model;
using WebApplication1.App_Code;
using Wuqi.Webdiyer;
using System.Data;

namespace WebApplication1.RecordManagement
{
    public partial class ToolRecordManagement : System.Web.UI.Page
    {
        private string var_RecordMode { get { return ViewState["RecordMode"].ToString(); } set { ViewState["RecordMode"] = value; } }
        private string var_WorkShop { get { return ViewState["WorkShop"].ToString(); } set { ViewState["WorkShop"] = value; } }
        private string var_ToolType { get { return ViewState["ToolType"].ToString(); } set { ViewState["ToolType"] = value; } }
        private string var_DistributionArea { get { return ViewState["DistributionArea"].ToString(); } set { ViewState["DistributionArea"] = value; } }
        private DateTime var_StartTime { get { return DateTime.Parse(ViewState["StartTime"].ToString()); } set { ViewState["StartTime"] = value; } }
        private DateTime var_StopTime { get { return DateTime.Parse(ViewState["StopTime"].ToString()); } set { ViewState["StopTime"] = value; } }
        private string var_ToolCode { get { return ViewState["ToolCode"].ToString(); } set { ViewState["ToolCode"] = value; } }
        private string var_ToolName { get { return ViewState["ToolName"].ToString(); } set { ViewState["ToolName"] = value; } }
        private ToolRecord tr;
        private Config c;

        public ToolRecordManagement()
        {
            tr = new ToolRecord();
            c = new Config();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRecordMode(this.DDL_RecordMode);
                BindDropDownList(this.DDL_WorkShop, enum_configType.WORK_SHOP);
                BindDropDownList(this.DDL_ToolType, enum_configType.TOOL_TYPE);
                BindDropDownList(this.DDL_DistrbutionArea, enum_configType.DISTRIBUTION_AREA);
            }
        }

        private void BindRecordMode(DropDownList ddl)
        {
            BindRecordMode_withoutNulll(ddl);
            ddl.Items.Insert(0, new ListItem("请选择"));
        }

        private void BindRecordMode_withoutNulll(DropDownList ddl)
        {
            IList<ListItem> list = new List<ListItem>();
            foreach (string str in Enum.GetNames(typeof(enum_Record_Mode)))
            {
                list.Add(new ListItem(str));
            }
            ddl.DataSource = list;
            ddl.DataBind();
        }

        public void BindDropDownList(DropDownList ddl, enum_configType ct)
        {
            BindDropDownList_withoutNull(ddl, ct);
            ddl.Items.Insert(0, new ListItem("请选择", ""));
        }

        public void BindDropDownList_withoutNull(DropDownList ddl, enum_configType ct)
        {
            ddl.DataSource = c.Config_GetAll_forSingleType(ct);
            ddl.DataTextField = "ItemName";
            ddl.DataValueField = "ItemName";
            ddl.DataBind();
        }

        private void BindToolRecord()
        {
            ToolRecordInfo tri = new ToolRecordInfo
            {
                Record_Mode = var_RecordMode.Equals("请选择") ? null : var_RecordMode,
                Work_Shop = var_WorkShop.Equals("请选择") ? null : var_WorkShop,
                Tool_Type = var_ToolType.Equals("请选择") ? null : var_ToolType,
                Distribution_Area = var_DistributionArea.Equals("请选择") ? null : var_DistributionArea,
                Tool_Code = string.IsNullOrEmpty(var_ToolCode) ? null : var_ToolCode,
                Tool_Name = string.IsNullOrEmpty(var_ToolName) ? null : var_ToolName,
            };
            IList<ToolRecordInfo> list = tr.ToolRecord_GetbyMultiFact(tri, var_StartTime, var_StopTime);
            //this.GridView1.DataSource = list;
            //this.GridView1.DataBind();
            //初始化分页数据源实例
            PagedDataSource pds = new PagedDataSource();
            //设置总行数
            AspNetPager1.RecordCount = list.Count;
            //设置分页的数据源
            pds.DataSource = list;
            //设置当前页
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //设置每页显示页数
            pds.PageSize = AspNetPager1.PageSize;
            //启用分页
            pds.AllowPaging = true;
            //设置GridView的数据源为分页数据源
            GridView1.DataSource = pds;
            //绑定GridView
            GridView1.DataBind();

            this.Lbl_RecordsCount.Text = list.Count.ToString();
            int amount_input = 0;
            int amount_output = 0;
            int amount_writeoff = 0;
            foreach (ToolRecordInfo trinfo in list)
            {
                amount_input += trinfo.Amount_Input;
                amount_output += trinfo.Amount_Output;
                amount_writeoff += trinfo.Amount_WriteOff;
            }
            this.Lbl_AmountInput.Text = amount_input.ToString();
            this.Lbl_AmountOutput.Text = amount_output.ToString();
            this.Lbl_AmountWriteOff.Text = amount_writeoff.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var_RecordMode =  this.DDL_RecordMode.SelectedItem.Text;
            var_WorkShop = this.DDL_WorkShop.SelectedItem.Text;
            var_ToolType = this.DDL_ToolType.SelectedItem.Text;
            var_DistributionArea = this.DDL_DistrbutionArea.SelectedItem.Text;
            var_StartTime = DateTime.Parse(this.Tb_StartTime.Text + " 00:00:00");
            var_StopTime = DateTime.Parse(this.Tb_StopTime.Text + " 00:00:00");
            var_ToolCode = this.Tb_ToolCode.Text.Trim();
            var_ToolName = this.Tb_ToolName.Text.Trim();
            BindToolRecord();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindToolRecord();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindToolRecord();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindToolRecord();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            ToolRecordInfo tri = new ToolRecordInfo();
            tri.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            tri.Record_Date = DateTime.Parse(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Tb_RecordDate"))).Text.Replace(" 0:00:00","")+ " 00:00:00");
            tri.Record_Mode = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLRecord_Mode"))).SelectedValue;
            tri.Work_Shop = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLWork_Shop"))).SelectedValue;
            tri.Tool_Type = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLTool_Type"))).SelectedValue;
            tri.ProductType = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim();
            tri.ProductTypeName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim();
            tri.Tool_Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.Trim();
            tri.Tool_Code = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim();
            Tool_Code tc = new Tool_Code();
            Tool_CodeInfo tci = new Tool_CodeInfo { Tool_Code = tri.Tool_Code };
            tri.Tool_Type_Spec = tc.Tool_Code_GetbyMultiFact(tci).Count == 0 ? "" : tc.Tool_Code_GetbyMultiFact(tci)[0].Tool_Type_Spec;
            tri.Tool_EnableSN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text.Trim();
            switch ((enum_Record_Mode)Enum.Parse(typeof(enum_Record_Mode), tri.Record_Mode))
            {
                case enum_Record_Mode.入库:
                    tri.Amount=int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[11].Controls[0])).Text.Trim());
                    break;
                case enum_Record_Mode.出库:
                    tri.Amount=int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[12].Controls[0])).Text.Trim());
                    break;
                case enum_Record_Mode.报废:                    
                    tri.Amount=int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[13].Controls[0])).Text.Trim());
                    break;
            }
            tri.Taker = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[14].Controls[0])).Text.Trim();
            tri.Taker_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[15].Controls[0])).Text.Trim();
            tri.Distribution_Area = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLDistribution_Area"))).SelectedValue;
            tri.Tool_State = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLTool_State"))).SelectedValue;
            tri.Repair_Record = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLRepair_Record"))).SelectedValue;
            tri.Remark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[19].Controls[0])).Text.Trim();

            tr.Update(tri);
            GridView1.EditIndex = -1;
            BindToolRecord();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            tr.Delete(ID);
            BindToolRecord();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
               
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[21].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
                }
                #region 型号规格内容显示过长 可将此代码放开 
                //Label label = (Label)e.Row.FindControl("lbTTSp");
                //string Str = label.Text; //第9列内容
                //if (Str.Length > 9) //第9列内容大于9个
                //{
                //    label.Text = Str.Substring(0, 9) + "..."; //截取9个显示，其他用“...”号代替
                //    label.ToolTip = Str;//鼠标放上去显示全部信息
                //} 
                #endregion 
            }
           
            if (((DropDownList)e.Row.FindControl("DDLRecord_Mode")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("DDLRecord_Mode");
                BindRecordMode_withoutNulll(ddl);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HDFRecord_Mode")).Value.Trim();
            }
            if (((DropDownList)e.Row.FindControl("DDLWork_Shop")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("DDLWork_Shop");
                BindDropDownList_withoutNull(ddl, enum_configType.WORK_SHOP);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HDFWork_Shop")).Value.Trim();
            }
            if (((DropDownList)e.Row.FindControl("DDLTool_Type")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("DDLTool_Type");
                BindDropDownList_withoutNull(ddl, enum_configType.TOOL_TYPE);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HDFTool_Type")).Value.Trim();
            }
            if (((DropDownList)e.Row.FindControl("DDLDistribution_Area")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("DDLDistribution_Area");
                BindDropDownList_withoutNull(ddl, enum_configType.DISTRIBUTION_AREA);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HDFDistribution_Area")).Value.Trim();
            }
            if (((DropDownList)e.Row.FindControl("DDLTool_State")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("DDLTool_State");
                BindDropDownList_withoutNull(ddl, enum_configType.TOOL_STATE);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HDFTool_State")).Value.Trim();
            }
            if (((DropDownList)e.Row.FindControl("DDLRepair_Record")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("DDLRepair_Record");
                BindDropDownList_withoutNull(ddl, enum_configType.REPAIR_RECORD);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HDFRepair_Record")).Value.Trim();
            }
        }

        protected void ButAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/RecordManagement/ToolRecordAdd.aspx");
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //调用绑定分页和GridView
            AspNetPager1.CurrentPageIndex = (sender as AspNetPager).CurrentPageIndex;
            BindToolRecord();
        }

    }
}
